package com.company.test16;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Administrator on 2016.6.11.
 */
public class Test16_01 extends JFrame{
    private JButton jbtEnlarge=new JButton("Enlarge");
    private JButton jbtShrink=new JButton("Shrink");
    private CirclePanel canvas=new CirclePanel();

    public Test16_01(){
        JPanel panel=new JPanel();
        this.jbtEnlarge.addActionListener(new EnlargeListener());
        this.jbtShrink.addActionListener(new ShrinkListener());
        panel.add(jbtEnlarge);
        panel.add(jbtShrink);

        this.add(canvas,BorderLayout.CENTER);
        this.add(panel,BorderLayout.SOUTH);
    }

    public static void main(){
        Test16_01 frame=new Test16_01();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setSize(200,200);
        frame.setTitle("Circle");
        frame.setVisible(true);
    }

    class EnlargeListener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            canvas.enlarge();
        }
    }

    class ShrinkListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            canvas.shrink();
        }
    }

    class CirclePanel extends JPanel{
        private int radius=5;

        public void enlarge(){
            radius++;
            repaint();
        }

        public void shrink(){
            radius--;
            repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawOval(getWidth()/2-radius,getHeight()/2-radius,2*radius,2*radius);
        }
    }
}
